package com.espirit.eap.util;

import javax.annotation.Resource;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.espirit.eap.manager.DepartmentService;
import com.espirit.eap.manager.EapI18nData;
import com.espirit.eap.manager.EntityInterceptor;
import com.espirit.eap.manager.EspiritSystemLogRegularService;
import com.espirit.eap.manager.ModelExportMappingService;
import com.espirit.eap.manager.workflow.WFInstanceService;
import com.espirit.eap.manager.workflow.WFTemplateService;
import com.espirit.eap.manager.workflow.WorkflowAware;
import com.espirit.eap.pagelayout.ModelService;
import com.espirit.eap.sa.EmployeeService;
import com.espirit.eap.sa.LoginUser;
import com.espirit.eap.sa.PrivilegeService;
import com.espirit.eap.sa.User;
import com.espirit.eap.sa.UserService;
import com.googlecode.cswish.Config;
import com.googlecode.cswish.struts.Initialization;
import com.googlecode.cswish.struts.conversion.DefaultDateConverter;
import com.googlecode.cswish.struts.spring.Executor;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.hr.HrEntityInteceptor;
import com.hr.HrSetting;
import com.hr.LeaveAppItem;
import com.hr.OtItem;
import com.hr.SchedualScopeService;
import com.hr.init.DBAService;

@Service
@Lazy(value=false)
public class BizSystemStartupManager implements Initialization {

	@Resource
	private ObjectFactory objectFactory;
	
	@Resource
	Config config;
	
	@Resource
	DBAService dbaService;
	
	@Resource
	EapI18nData i18nData;
	
	@Override
	public void initSite() {
		// data converter
//		HrSettingService systemSettingService = objectFactory.getBean(HrSettingService.class, true);
		DefaultDateConverter defaultDateConverter = objectFactory.getBean(DefaultDateConverter.class, true);
//		BigDecimalConverter bigDecimalConverter = objectFactory.getBean(BigDecimalConverter.class, true);	
		defaultDateConverter.setDateFormat(HrSetting.TIME_FORMAT);
//		defaultDateConverter.setDateFormat(systemSettingService.getDateFormat());
//		bigDecimalConverter.setPriceScale(systemSettingService.getPriceScale());
//		bigDecimalConverter.setAmountScale(systemSettingService.getAmountScale());
		
		// model search callback
		ModelExportMappingService modelExportMappingService = objectFactory.getBean(ModelExportMappingService.class, true);
		modelExportMappingService.registerExportModeMapping();
		
		EspiritSystemLogRegularService espiritSystemLogRegularService = objectFactory.getBean(EspiritSystemLogRegularService.class, true);
		espiritSystemLogRegularService.registerSystemLogRegular();
		
		// Entiry Interceptor
		EntityInterceptor entityInterceptor = objectFactory.getBean(EntityInterceptor.class, true);
		HrEntityInteceptor defaultInterceptor = objectFactory.getBean(HrEntityInteceptor.class, true);
		entityInterceptor.setInterceptor(defaultInterceptor);
		
		SchedualScopeService schedualScopeService = objectFactory.getBean(SchedualScopeService.class, true);
		schedualScopeService.registerModelSearchType();
		
		EmployeeService employeeService = objectFactory.getBean(EmployeeService.class, true);
		employeeService.registerModelSearchType();
		
		// work flow
		registerWFTemplate();
		PrivilegeService privilegeService = objectFactory.getBean(PrivilegeService.class, true);
		privilegeService.registerPrivileges();
		
		DepartmentService departmentService = objectFactory.getBean(DepartmentService.class, true);
		departmentService.initData();
		
//		WebsiteService websiteService = objectFactory.getBean(WebsiteService.class, true);
//		websiteService.set(dbaService);
	}
	
	private void registerWFTemplate() {
		WFTemplateService wfTemplateService = objectFactory.getBean(WFTemplateService.class, true);
		ModelService modelService = objectFactory.getBean(ModelService.class, true);
		
		//must implements workflowaware interface
		Class [] srcItems = new Class[] {LeaveAppItem.class,
										OtItem.class};
		
		for(Class clazz : srcItems){
			wfTemplateService.register(clazz, modelService.getI18nValue(clazz));
		}
		
		WFInstanceService wfInstanceService = objectFactory.getBean(WFInstanceService.class, true);
		
		wfInstanceService.registerNotice(new Executor<Object, Object[]>() {
			
			@Override
			public Object execute(Object[] parameters) {
				WorkflowAware workflowAware = (WorkflowAware)parameters[0];
				LoginUser to = (LoginUser)parameters[1];
				UserService userService = objectFactory.getBean(UserService.class, true);
				User toUser = userService.getUserByLoginUser(to.getName());
				
				return null;
			}
		});
	}
    final String[] productName = new String[] {"hr"};
	@Override
	public String[] getProductName() {
		return productName;
	}
}
